<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="MySQL,">










<meta name="description" content="目前在对现场的项目升级的时候，发现有些项目部署时间久远，数据库很老，跟最新的数据库相去甚远。如何升级数据库与最新代码保持一致，成为一个大问题。经过研究，决定采用数据库比对的方式，对更新老库的数据表和字段，主要是两个方面：一是比对新老数据库的数据表，新建老库不存在的表；二是比对新老数据库的表字段，新建老库不存在的字段。 查询两个数据库的表和字段创建两个临时表，分别存放查询出来的两个数据库的表信息。">
<meta name="keywords" content="MySQL">
<meta property="og:type" content="article">
<meta property="og:title" content="比较两个数据库的表和字段">
<meta property="og:url" content="http://vosamo.gitee.io/blog/2019/07/20/比较两个数据库的表和字段/index.html">
<meta property="og:site_name" content="VOSAMO">
<meta property="og:description" content="目前在对现场的项目升级的时候，发现有些项目部署时间久远，数据库很老，跟最新的数据库相去甚远。如何升级数据库与最新代码保持一致，成为一个大问题。经过研究，决定采用数据库比对的方式，对更新老库的数据表和字段，主要是两个方面：一是比对新老数据库的数据表，新建老库不存在的表；二是比对新老数据库的表字段，新建老库不存在的字段。 查询两个数据库的表和字段创建两个临时表，分别存放查询出来的两个数据库的表信息。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-07-20T06:42:56.226Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="比较两个数据库的表和字段">
<meta name="twitter:description" content="目前在对现场的项目升级的时候，发现有些项目部署时间久远，数据库很老，跟最新的数据库相去甚远。如何升级数据库与最新代码保持一致，成为一个大问题。经过研究，决定采用数据库比对的方式，对更新老库的数据表和字段，主要是两个方面：一是比对新老数据库的数据表，新建老库不存在的表；二是比对新老数据库的表字段，新建老库不存在的字段。 查询两个数据库的表和字段创建两个临时表，分别存放查询出来的两个数据库的表信息。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://vosamo.gitee.io/blog/2019/07/20/比较两个数据库的表和字段/">





  <title>比较两个数据库的表和字段 | VOSAMO</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">VOSAMO</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://vosamo.gitee.io/blog/blog/2019/07/20/比较两个数据库的表和字段/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vosamo">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="VOSAMO">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">比较两个数据库的表和字段</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-07-20T11:56:54+08:00">
                2019-07-20
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/笔记/" itemprop="url" rel="index">
                    <span itemprop="name">笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>目前在对现场的项目升级的时候，发现有些项目部署时间久远，数据库很老，跟最新的数据库相去甚远。如何升级数据库与最新代码保持一致，成为一个大问题。经过研究，决定采用数据库比对的方式，对更新老库的数据表和字段，主要是两个方面：一是比对新老数据库的数据表，新建老库不存在的表；二是比对新老数据库的表字段，新建老库不存在的字段。</p>
<h2 id="查询两个数据库的表和字段"><a href="#查询两个数据库的表和字段" class="headerlink" title="查询两个数据库的表和字段"></a>查询两个数据库的表和字段</h2><p>创建两个临时表，分别存放查询出来的两个数据库的表信息。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TEMPORARY</span> <span class="keyword">TABLE</span> <span class="keyword">IF</span> <span class="keyword">EXISTS</span> TA;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TEMPORARY</span> <span class="keyword">TABLE</span> TA </span><br><span class="line"><span class="keyword">SELECT</span> TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_TYPE <span class="keyword">FROM</span> information_schema.<span class="string">`COLUMNS`</span> <span class="keyword">WHERE</span> TABLE_SCHEMA=<span class="string">'数据库1'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TEMPORARY</span> <span class="keyword">TABLE</span> <span class="keyword">IF</span> <span class="keyword">EXISTS</span> TB;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TEMPORARY</span> <span class="keyword">TABLE</span> TB </span><br><span class="line"><span class="keyword">SELECT</span> TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_TYPE <span class="keyword">FROM</span> information_schema.<span class="string">`COLUMNS`</span> <span class="keyword">WHERE</span> TABLE_SCHEMA=<span class="string">'数据库2'</span>;</span><br></pre></td></tr></table></figure>
<p>如上，临时表TA保存了数据库1的表名、字段名、字段类型信息；临时表TB保存数据库2的表名、字段名、字段类型。根据这些信息就可以进行两个数据库的比对了。</p>
<a id="more"></a>
<h2 id="新增数据表"><a href="#新增数据表" class="headerlink" title="新增数据表"></a>新增数据表</h2><p>查询出老的数据库缺少的表，然后新建。</p>
<h3 id="比对两个数据库的表"><a href="#比对两个数据库的表" class="headerlink" title="比对两个数据库的表"></a>比对两个数据库的表</h3><p>首先，比对两个数据库的表，找出数据库1（新库）存在而数据库1不存在的数据表。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span> a.TABLE_NAME <span class="keyword">FROM</span> TA a <span class="keyword">LEFT</span> <span class="keyword">JOIN</span> TB b <span class="keyword">ON</span> a.TABLE_NAME = b.TABLE_NAME <span class="keyword">WHERE</span> b.TABLE_NAME <span class="keyword">IS</span> <span class="literal">NULL</span>;</span><br></pre></td></tr></table></figure>
<p>上述SQL可以查询出新库多的表：</p>
<p>bmc_backup<br>bmc_ctg<br>bmc_data<br>bmc_db<br>bmc_dn<br>bmc_flow_detail<br>bmc_flow_index<br>bmc_flow_pricediscount<br>bmc_flow_product<br>bmc_flow_type<br>bmc_form_config<br>bmc_lb</p>
<h3 id="新建缺失表"><a href="#新建缺失表" class="headerlink" title="新建缺失表"></a>新建缺失表</h3><p>使用shell脚本导出上述缺失的表：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#!/bin/bash</span><br><span class="line">for i in `cat tbs`</span><br><span class="line">do</span><br><span class="line">mysqldump -hxxxx -uroot -pxxx 数据库1 $i &gt;&gt; dbdump.sql</span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<p>将缺失的表保存到一个文本文件中，比如tbs，然后循环调用mysqldump导出表结构和数据，将导出的sql文件在老数据库中运行一下，就完成了缺失表的新建。</p>
<h2 id="增加缺失字段"><a href="#增加缺失字段" class="headerlink" title="增加缺失字段"></a>增加缺失字段</h2><p>前面是如何新建缺失的表，对于两个数据库都存在的表但是字段不一致的情况，也需要补齐缺失的字段。补齐字段和新建表类似，只不过比对的时候需要比对字段名。</p>
<h3 id="比对两个数据库的字段"><a href="#比对两个数据库的字段" class="headerlink" title="比对两个数据库的字段"></a>比对两个数据库的字段</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	a.TABLE_NAME,</span><br><span class="line">	a.COLUMN_NAME,</span><br><span class="line">	a.COLUMN_TYPE</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	TA a</span><br><span class="line"><span class="keyword">LEFT</span> <span class="keyword">JOIN</span> TB b <span class="keyword">ON</span> a.TABLE_NAME = b.TABLE_NAME</span><br><span class="line"><span class="keyword">AND</span> a.COLUMN_NAME = b.COLUMN_NAME</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	b.COLUMN_NAME <span class="keyword">IS</span> <span class="literal">NULL</span>;</span><br></pre></td></tr></table></figure>
<p>查询出来的内容包括数据库名、字段名和字段类型，然后复制到notepadd中通过列编辑功能，组装成alter table语句，在老的数据库中执行，就完成了缺失字段的增加。</p>
<p>或者也可以直接通过sql查询语句拼接出新增字段的语句：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	<span class="string">"alter table "</span>,</span><br><span class="line">	a.TABLE_NAME,</span><br><span class="line">	<span class="string">"add column "</span>,</span><br><span class="line">	a.COLUMN_NAME,</span><br><span class="line">	a.COLUMN_TYPE,</span><br><span class="line">	<span class="string">";"</span></span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	TA a</span><br><span class="line"><span class="keyword">LEFT</span> <span class="keyword">JOIN</span> TB b <span class="keyword">ON</span> a.TABLE_NAME = b.TABLE_NAME</span><br><span class="line"><span class="keyword">AND</span> a.COLUMN_NAME = b.COLUMN_NAME</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	b.COLUMN_NAME <span class="keyword">IS</span> <span class="literal">NULL</span>;</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/MySQL/" rel="tag"># MySQL</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2019/04/12/记录一个Cinder-volume无法删除的错误：ImageBusy-error-raised-while-deleting-rbd-volume/" rel="next" title="记录一个Cinder volume无法删除的错误：ImageBusy error raised while deleting rbd volume">
                <i class="fa fa-chevron-left"></i> 记录一个Cinder volume无法删除的错误：ImageBusy error raised while deleting rbd volume
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2019/07/30/SpringBoot集成MyBatis和mybatis-generator/" rel="prev" title="SpringBoot集成MyBatis和mybatis-generator">
                SpringBoot集成MyBatis和mybatis-generator <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/blog/images/avatar.gif" alt="vosamo">
            
              <p class="site-author-name" itemprop="name">vosamo</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">4</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#查询两个数据库的表和字段"><span class="nav-number">1.</span> <span class="nav-text">查询两个数据库的表和字段</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#新增数据表"><span class="nav-number">2.</span> <span class="nav-text">新增数据表</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#比对两个数据库的表"><span class="nav-number">2.1.</span> <span class="nav-text">比对两个数据库的表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#新建缺失表"><span class="nav-number">2.2.</span> <span class="nav-text">新建缺失表</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#增加缺失字段"><span class="nav-number">3.</span> <span class="nav-text">增加缺失字段</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#比对两个数据库的字段"><span class="nav-number">3.1.</span> <span class="nav-text">比对两个数据库的字段</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vosamo</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
